<?php

namespace app\api\model;

use app\api\model\ConfigSystem as Config;

class Privatechat extends Common
{
    static public function notice($to_user, $content, $data='', $k=0, $type = 0)
    {
        $user_id = Config::where(['title' => 'id'])->value('content');

        $chat = self::create([
            'userid' => $user_id,
            'touserid' => $to_user,
            'content' => $content,
            'data' => $data,
            'k' => $k,
            'single' => 1,
            'type' => $type,
            'front_id'=>time()
        ]);

        ChatLists::updateLists($user_id, $to_user, 1, 0, $content, 0);

        $url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '/apis/home/privatechat/';

        curl(str_replace('/index.php/apis', '/apis',$url) . 'getNewMsg', json_encode(['touserid' => $to_user, 'userid' => $user_id]), 0);
        curl(str_replace('/index.php/apis', '/apis',$url) . 'getOtherList', json_encode(['userid' => $to_user, 'count' => '', 'type' => '']), 0);
        curl(str_replace('/index.php/apis', '/apis',$url) . 'getreadcount', json_encode(['user_id' => $to_user]), 0);

        return $chat->id;
    }
    
    static public function tip($user_id, $to_user, $content, $data='', $single=0, $type = -1)
    {
        $chat = self::create([
            'userid' => $user_id,
            'touserid' => $to_user,
            'content' => $content,
            'data' => $data,
            'single' => $single,
            'type' => $type,
            'front_id'=>time()
        ]);

        ChatLists::updateLists($user_id, $to_user, 1, 0, $content, 0);

        $url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '/apis/home/privatechat/';

        curl(str_replace('/index.php/apis', '/apis',$url) . 'getNewMsg', json_encode(['touserid' => $to_user, 'userid' => $user_id]), 0);
        curl(str_replace('/index.php/apis', '/apis',$url) . 'getOtherList', json_encode(['userid' => $to_user, 'count' => '', 'type' => '']), 0);
        curl(str_replace('/index.php/apis', '/apis',$url) . 'getreadcount', json_encode(['user_id' => $to_user]), 0);

        return $chat->id;
    }

    static public function checkFree($free, $money, $recharge_user, $userid): array
    {
        $UserModel = new User;
        $GradeNobleModel = new GradeNoble;
        $GradeNobleUseModel = new GradeNobleUse;

        if ($recharge_user['noble_time'] > time() && $recharge_user['noble_grade']) {
            $gradeNoble = $UserModel->where(['grade' => $recharge_user['noble_grade']])->find();

            if ($gradeNoble) {
                $privilege = explode(',', $gradeNoble['privilege']);

                if (in_array(10, $privilege)) {
                    $privilegeValue = explode(',', $gradeNoble['value']);

                    if (isset($privilegeValue[0]) && $privilegeValue[0]) {
                        $userCount = $GradeNobleUseModel->where(['types' => 3, 'user_id' => $recharge_user['id'], 'create_time' => ['egt', strtotime(date('Ymd'))]])->count();

                        $privilegeValue[0] = intval($privilegeValue[0]);
                        if ($privilegeValue[0] > $userCount) {
                            $GradeNobleUseModel->insert(['types' => 3, 'user_id' => $recharge_user['id'], 'create_time' => time()]);

                            $free = 1;
                        }
                    }
                }
            }
        }

        if (!$free) {
            if ($recharge_user['free_chat']) {
                $UserModel->where(['id' => $recharge_user['id']])->setDec('free_chat');
            } else {
                if ($recharge_user['volley'] < $money) {
                    if ($recharge_user['id'] == $userid) {
                        return ['code' => 300, 'msg' => '余额不足，无法发送消息'];
                    } else {
                        return ['code' => 400, 'msg' => '对方余额不足，无法发送消息'];
                    }
                }
            }
        }

        return ['code' => 200, 'data' => ['free' => $free]];
    }

    static public function changeWord($content)
    {
        if (stripos($content, '接听') === 0 && stripos($content, '的视频通话') !== false) {
            $content = '接听视频通话';
        } else if (stripos($content, '接听') === 0 && stripos($content, '的语音通话') !== false) {
            $content = '接听语音通话';
        } else if (stripos($content, '向') === 0 && stripos($content, '发起视频通话') !== false) {
            $content = '发起视频通话';
        } else if (stripos($content, '向') === 0 && stripos($content, '发起语音通话') !== false) {
            $content = '发起语音通话';
        }

        return $content;
    }
}
